package com.wy.servlet.login;

import com.wy.utils.CookieUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet("/login/login.do")
public class Login extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie username = CookieUtils.getCookieByName("username",req);
        Cookie password = CookieUtils.getCookieByName("password",req);
        Cookie rememberMe = CookieUtils.getCookieByName("rememberMe",req);

        String username1 = req.getParameter("username");
        String password1 = req.getParameter("password");
        if ("username".equals(username1)&&"password".equals(password1)){
            req.getSession().setAttribute("user",username1);
/*            resp.getWriter().write("login success");
            String rememberMe1 = req.getParameter("rememberMe");
            if ("on".equals(rememberMe1)){
                username.setValue(username1);
                password.setValue(password1);
                rememberMe.setValue("on");
            }else {
                username.setMaxAge(0);
                password.setMaxAge(0);
                rememberMe.setMaxAge(0);
            }
            resp.addCookie(username);
            resp.addCookie(password);
            resp.addCookie(rememberMe);*/
            resp.sendRedirect(req.getContextPath() + "/user/userindex.jsp");
            return;
        }
        resp.getWriter().write("login failed");
    }
}
